package com.neusoft.neuiotms.dcom.controller.change;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.file.dto.FileDTO;
import com.neusoft.bizcore.file.service.FileService;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.neuiotms.dcom.dto.change.ChangeRegistrationDTO;
import com.neusoft.neuiotms.dcom.service.change.ChangeRegistrationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "变更登记与分发")
@RestController
@RequestMapping(value = "/api/w/change/registration")
public class ChangeRegistrationController {

    @Autowired
    private ChangeRegistrationService changeRegistrationService;

    @Autowired
    private FileService fileService;

    @ApiOperation("新增变更登记与分发工单")
    @PostMapping
    public ResultDTO<ChangeRegistrationDTO> create(@RequestBody final ChangeRegistrationDTO dto,
            final UserBean user) {
        if (user != null) {
            dto.setRegistrantCode(StringUtils.isEmpty(user.getUsername()) ? "admin" : user.getUsername());
            dto.setRegistrant(StringUtils.isEmpty(user.getName()) ? "admin" : user.getName());
        }
        return ResultDTO.success(this.changeRegistrationService.create(dto, user));
    }

    @ApiOperation("查看变更登记与分发工单")
    @GetMapping("/{workOrderNum}/{taskId}")
    public ResultDTO<ChangeRegistrationDTO> details(@PathVariable final String workOrderNum,
            @PathVariable final String taskId,
            final UserBean user) {
        return ResultDTO.success(this.changeRegistrationService.detail(workOrderNum, taskId, user));
    }

    @ApiOperation("上传文件")
    @PostMapping("/upload/attachment")
    public ResultDTO<FileDTO> upload(@RequestParam("file") final MultipartFile file) {
        final FileDTO doc = this.fileService.save(file);
        return ResultDTO.success(doc);
    }

    @ApiOperation("下载文件")
    @GetMapping("/download/{fsId}")
    public void download(@PathVariable final String fsId, final HttpServletResponse response) {
        this.fileService.download(fsId, response);
    }

    @ApiOperation("删除文件")
    @DeleteMapping("/delete/attachment/{fsId}")
    public void deleteAttachment(@PathVariable final String fsId) {
        this.changeRegistrationService.deleteMongoFile(fsId);
    }

}
